package com.ruoyi.common.utils.database;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.setting.Setting;
import com.ruoyi.common.enums.EsContent;
import com.ruoyi.common.enums.FormatType;
import com.ruoyi.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : zhenhc
 * @date : 2022-03-19 22:52
 **/
public class EsUtil {
    public static final String ELASTICSEARCH_CONFIG_PATH = "config/ElasticSearch.setting";
    public static final String url = new Setting(ELASTICSEARCH_CONFIG_PATH).getStr("url");

    /**
     * 创建索引
     * @param indicName 索引名称
     * @return
     */
    public static String createIndic(String indicName){
        String body = HttpRequest.put(url + "/" + indicName)
                .execute()
                .body();
        return body;
    }
    /**
     * 获取es信息
     * @return
     */
    public static String get(EsContent esContent, FormatType formatType){
        String formatTypeInfo = formatType.getInfo();
        String esContentInfo = esContent.getInfo();
        if ("json".equals(formatTypeInfo))
            formatTypeInfo = "format="+formatTypeInfo;
        String body = HttpRequest.get(url + esContentInfo + "?"+formatTypeInfo)
                .execute()
                .body();
        return body;
    }
    /**
     * @param sql sql脚本
     * @return
     */
    public static List<Map<String,Object>> searchBySql(String sql){
        List<Map<String,Object>> list = new ArrayList<>();
        HashMap<String, Object> params = new HashMap<>();
        params.put("query",sql);
        String body = HttpRequest.get(url+"/_sql?format=json")
                .body(JSONUtil.toJsonStr(params))
                .execute()
                .body();
        JSONObject result = JSONUtil.parseObj(body);
        JSONArray columns = result.getJSONArray("columns");
        JSONArray rows = result.getJSONArray("rows");
        for (int i=0;i<rows.size();i++){
            JSONArray row = rows.getJSONArray(i);
            HashMap<String, Object> map = new HashMap<>();
            for (int j=0;j<columns.size();j++){
                map.put(StringUtils.toCamelCase(columns.getJSONObject(j).getStr("name")),row.get(j));
            }
            list.add(map);
        }
        return list;
    }

    public static String searchBySql(String sql,FormatType formatType){
        String formatTypeInfo = formatType.getInfo();
        if ("json".equals(formatTypeInfo))
            formatTypeInfo = "format="+formatTypeInfo;
        List<Map<String,Object>> list = new ArrayList<>();
        HashMap<String, Object> params = new HashMap<>();
        params.put("query",sql);
        String body = HttpRequest.get(url+"/_sql?"+formatTypeInfo)
                .body(JSONUtil.toJsonStr(params))
                .execute()
                .body();
        return body;
    }


    
}
